<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width,minimum-scale=1.0,maximum-scale=1.0,initial-scale=1.0" />
    <title>沃闪购</title>
    <link rel="stylesheet" href="../css/normalize.css" />
    <!--<link rel="stylesheet" href="index.css"/>-->
    <!--<link rel="stylesheet" href="../index.css"/>-->
    <link rel="icon" href="../img/bitbug_favicon.ico" type="image/x-icon" />
    <link rel="stylesheet" href="../index_format.css" />
    <link rel="stylesheet" href="../css/layer.css" />
    <link rel="stylesheet" href="../css/jquery.idcode.css" />
    <script type="text/javascript" src="../js/jquery.min.js"></script>
    <script type="text/javascript" src="../js/jquery.idcode.js"></script>
    <script type="text/javascript" src="../js/rem.js"></script>
    <script type="text/javascript" src="../js/tool.js"></script>
    <script>
        document.documentElement.style.height = window.innerHeight + 'px';
        window.alert = function(name) {
            var iframe = document.createElement("IFRAME");
            iframe.style.display = "none";
            iframe.setAttribute("src", 'data:text/plain,');
            document.documentElement.appendChild(iframe);
            window.frames[0].window.alert(name);
            iframe.parentNode.removeChild(iframe);
        }
    </script>
</head>

<body onload="preventMove()">
    <div class="header">
        <div><img src="../img/wo-logo.png" alt="" /></div>
        <div class="wo-topic"> </div>
    </div>
    <div class="container">
        <div class="flex-box">
            <div class="choose-login">
                <form id="loginForm" action="">
                    <ul class="form-list">
                        <li><label for="wo_phone">用户名：</label><input id="wo_phone" name="userName" type="text" autofocus value="" placeholder="请输入11位手机号" /></li>
                        <li><label for="wo_pwd">密码：</label><input id="wo_pwd" name="password" type="password" value="" placeholder="请输入6位以上密码" /></li>
                        <li><label for="wo_code">验证码：</label><input id="wo_code" name="wo_code" class="wo-code" type="text" value="" placeholder="" /><i id="getCode" class="get-code">eeee</i></li>
                    </ul>
                </form>
                <div class="margin-center"></div>
                <div class="btn-group">
                    <a id="loginBtn" href="#" class="btn-style">登录</a>
                    <a href="register.html" class="btn-style">用户注册</a>
                    <!-- <a href="reset_password.html" class="btn-style">忘记密码</a> -->
                </div>
                <!-- <div><a href="register.html" class="btn-register">新用户注册</a></div> -->
            </div>
        </div>
    </div>
    <script type="text/javascript" src="../js/layer.js"></script>

    <script>
        var settings = {
            e: 'getCode',
            codeType: {
                name: 'follow',
                len: 4
            },
            codeTip: '',
            inputID: 'wo_code' //这个就是你页面输入验证码的文本框ID
        };
        $.idcode.setCode(settings);
        //测试验证码
        // $("#btns").click(function() {
        //     var IsBy = $.idcode.validateCode();
        //     alert(IsBy);
        //     console.log(IsBy);
        // });

        function layer_msg(mgs) {
            layer.open({
                content: mgs,
                skin: 'msg',
                time: 2
            })
        }

        var bes_login = {
            host_server: getServerHost(),
            url: '/api/shopping/login',
            loginForm: '#loginForm',
            loginBtn: '#loginBtn',
            wo_phone: "#wo_phone",
            wo_pwd: '#wo_pwd',
            init: function() {
                this.loginPage();
                this.getCode();
            },
            check: function() {
                var context = this;
                //验证手机
                function testPhone() {
                    var myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1}))+\d{8})$/;
                    if (!myreg.test($(context.wo_phone).val())) {
                        layer_msg('请输入有效的手机号码！')
                        return false;
                    }
                    return true;
                }

                function testCode() {
                    var key = $.idcode.validateCode()
                    if (!key) {
                        layer_msg('验证码错误');
                        return false;
                    }
                    return true;
                }
                return testPhone() && testCode();
            },
            getCode: function() {
                var context = this;
            },
            loginPage: function() {
                var context = this;
                $(context.loginBtn).click(function(e) {
                    e.preventDefault();
                    if (context.check() && $(context.wo_phone).val() && $(context.wo_pwd).val()) {
                        var postData = {};
                        //                    postData.data=JSON.stringify($(context.loginForm).serializeArray());
                        $.ajax({
                            type: 'POST',
                            url: context.host_server + context.url,
                            data: JSON.stringify(arry2json($(context.loginForm).serializeArray())),
                            contentType: "application/json;charset=UTF-8",
                            success: function(response) {
                                sessionStorage.setItem("user_info", JSON.stringify(response));
                                switch (response.code) {
                                    case '200':
                                        sessionStorage.setItem('res', JSON.stringify(response.res));
                                        //                                    alert("登录成功");
                                        layer_msg('登录成功')
                                        setTimeout(function() {
                                            location.href = "poster.html";
                                        }, 1000);
                                        break;
                                    case '0001':
                                        layer_msg(response.msg)
                                        break;
                                    case '0002':
                                        layer_msg(response.msg)
                                        break;
                                    case '0003':
                                        layer_msg(response.msg)
                                        break;
                                    case '0004':
                                        layer_msg(response.msg)
                                        break;
                                    case '0005':
                                        layer_msg(response.msg)
                                        break;
                                    case '0006':
                                        layer_msg(response.msg)
                                        break;
                                    case '0007':
                                        layer_msg(response.msg)
                                        break;
                                    default:
                                        layer_msg('未知错误')
                                        break;
                                }
                            },
                            error: function(repsonse) {
                                console.log(repsonse);
                                alert('登陆出现问题，请稍后再试')
                            }
                        })
                    } else {
                        // layer_msg('请输入账号和密码');
                        // alert('请输入后再登陆');
                    }

                })
            }

        }
        bes_login.init();
    </script>
</body>

</html>
